<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
<head>
  <title>iSwitch - Remote Instrument Switcher</title>
  <meta http-equiv="cache-control" content="no-cache" />
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <link type="text/css" href="/css/default.css" media="screen" rel="stylesheet" />

  <!-- required scripts -->
  <script type="text/javascript" src="/js/jquery-1.5.1.min.js"></script>

  <style type="text/css">
    body {
      background-color: black;
    }
    table {
      background-color: #ddf;
      border: 1px solid #224;
      margin-left: auto;
      margin-right: auto;
    }
    td {
      font-size: 11px;
      height: 20px;
      padding: 8px;
    }
    .id {
      color: #555;
    }
    .highlight td{
      background-color: #333377;
      font-weight: bold;
      color: white;
    }
    tr {
      cursor: pointer;
    }
    tr:hover{
      background-color: #AAAAFF;
    }
  </style>
</head>
<body>

  <div id="placeholder"></div>

  <script type="text/javascript">
    var client_id = ~{do
        local client = L:generate_client()
        OUT = client.id
    end};

    var autoupdate = true;
    var au_interval = 500; // ms

    var initial = ~{renoise.song().selected_instrument_index};

    function highlight(id){
      $("tr").removeClass("highlight");
      $("#i"+id).addClass("highlight");
    }

    function set_selected_instrument(id){
      initial = id;
      highlight(id);
    }
    
    function handle_callback(c){
      if (typeof c == "function"){
        c();
      } else if (typeof c == "Array") {
        $.each(c, function(k,v){
          handle_callback(c);
        });
      }
    }

    function load_instrument_table(callback){
      //Load instrument list into an HTML table
      $.get('instruments.lua', function(data){

        //Display Content
        $("#placeholder").html(data);
        
        highlight(initial);

        //Execute Callbacks
        handle_callback(callback);
      });
    }

    function update(data){
      data = $.parseJSON(data);
      $.each(data, function(k,event){
        switch(event.key) {
          case "inst": set_selected_instrument(event.value); break;
          case "song_change": load_instrument_table(); break;
        }
      });
    }

    function loop(){
      if (autoupdate){
        $.get('iswitch.lua', {client_id: client_id}, function(data){
          update(data);
        });
      }
      setTimeout('loop()', au_interval);
    }

    $(document).ready(function(){
      $('body').prepend('<div id="gmenu"></div>');
      $('#gmenu').load('/menu.html');

      $('tr').live('click', function(){
        var id = $(this).attr('id');
        var i = id.substr(1);
        $.post("iswitch.lua", {inst:i}, function(){
          highlight(i);
        });
      });

      load_instrument_table(loop);
    });
  </script>
</body>
</html>

